//
//  XJ_HomeViewController.m
//  XunJieProject
//
//  Created by xue on 2018/6/7.
//  Copyright © 2018年 Soowin. All rights reserved.
//

#import "XJ_HomeViewController.h"
#import "XJ_HomeCardListMode.h"
#import "XJ_HomePalyImageModel.h"
#import "XJ_CardListCell.h"
#import "TYCyclePagerView.h"
#import "TYCyclePagerViewCell.h"
#import "TYPageControl.h"
#import "XJ_HomeNewsViewController.h"
#import "XJ_AddCreditCardViewController.h"
#import "XJ_HomeDetaiViewController.h"
#import "XJ_JiHualeiXingView.h"
#import "XJ_QuickRepaymentViewController.h"
#import "XJ_ReturnListViewController.h"
#import "XJ_UnsubmissionPlanListViewController.h"
#import "XJ_ApplicationDetailViewController.h"//web页
#import "XJ_AnnounceListModel.h"
#import "XJ_RealNameAuthViewController.h"
#import "LMJScrollTextView2.h"
#import "XJ_RealNameAuthView.h"
#define  TOP_LUNBO 1200
@interface XJ_HomeViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate,TYCyclePagerViewDataSource, TYCyclePagerViewDelegate,LMJScrollTextView2Delegate>
{
    
    UIView *notificationView;
    BOOL isOpen;
    NSArray * Imagedatas;
    NSInteger selectedCellNum;//点击填加还款计划所选中的cell
    NSInteger identifyFlag;//是否实名认证 1：是 0：否
    NSString * userName;
    NSString * userIDNumber;//身份证号码
    NSInteger channelId;//1:炎煌 2:非凡 3:易宝
}

@property (nonatomic, strong) TYCyclePagerView *pagerView;
@property (nonatomic, strong) TYCyclePagerView *topPagerView;
@property (nonatomic, strong) TYPageControl *pageControl;

@property (nonatomic, strong)UIScrollView * backScroll;//底层scroll
@property (nonatomic, strong)UIScrollView * bannerScroll;//轮播图
@property (nonatomic, strong) LMJScrollTextView2 * scrollTextView;
@property (nonatomic, strong)UIView * emptyview;
@property (nonatomic, strong)UIView * tableTopView;
@property (nonatomic, strong)UITableView * cardTable;
@property (nonatomic, strong)UIButton *  addAcreditCardButton;

@property (nonatomic, strong)NSMutableArray * cardListArray;
@property (nonatomic, strong)NSMutableArray * playImagesArray;
@property (nonatomic, strong)NSMutableArray * announceListArray;
@property (nonatomic, strong)UIControl * leiXingControl;
@end

@implementation XJ_HomeViewController

- (NSMutableArray *)announceListArray{
    
    if (_announceListArray == nil) {
        
        _announceListArray = [NSMutableArray array];
    }
    
    return _announceListArray;
}
- (NSMutableArray *)cardListArray{
    
    if (_cardListArray == nil) {
        
        _cardListArray = [NSMutableArray array];
        
    }
    
    return _cardListArray;
    
}
- (NSMutableArray *)playImagesArray{
    
    if (_playImagesArray == nil) {
        
        _playImagesArray = [NSMutableArray array];
        
    }
    
    return _playImagesArray;
    
}

- (UITableView *)cardTable{
    
    if (_cardTable == nil) {
        
        _cardTable = [[UITableView alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(self.tableTopView.frame), SCREEN_WIDTH - 30, 0) style:UITableViewStylePlain];
        _cardTable.delegate = self;
        _cardTable.dataSource = self;
        _cardTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        _cardTable.backgroundColor = [UIColor colorWithHexString:@"f2f2f1"];
    }
    
    return _cardTable;
}

- (UIButton *)addAcreditCardButton{
    
    if (_addAcreditCardButton == nil) {
        
        _addAcreditCardButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _addAcreditCardButton.frame =CGRectMake(15, CGRectGetMaxY(self.cardTable.frame), SCREEN_WIDTH - 30, 45);
        [_addAcreditCardButton setBackgroundColor:[UIColor whiteColor]];
        [_addAcreditCardButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _addAcreditCardButton.layer.cornerRadius = 5;
        _addAcreditCardButton.layer.borderWidth = 1;
        _addAcreditCardButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
        
        [_addAcreditCardButton setImage:[UIImage imageNamed:@"jiahao"] forState:UIControlStateNormal];
        [_addAcreditCardButton setTitle:@" 添加信用卡" forState:UIControlStateNormal];
        [_addAcreditCardButton setTitleColor:[UIColor colorWithHexString:@"2b2b2c"] forState:UIControlStateNormal];
        _addAcreditCardButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_addAcreditCardButton addTarget:self action:@selector(addCardAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _addAcreditCardButton;
}


- (UIView *)tableTopView{
    
    if (_tableTopView == nil) {
        
        _tableTopView = [[UIView alloc]initWithFrame:CGRectMake(0,40+SCREEN_WIDTH/2, SCREEN_WIDTH, 50)];
        _tableTopView.backgroundColor = [UIColor colorWithHexString:@"f2f2f1"];
        
        UIButton * addCardBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        addCardBtn.frame = CGRectMake(15, 15, 25, 25);
        [addCardBtn setImage:[UIImage imageNamed:@"add_bg"] forState:UIControlStateNormal];
        [addCardBtn addTarget:self action:@selector(addCardAction:) forControlEvents:UIControlEventTouchUpInside];
        [_tableTopView addSubview:addCardBtn];
        
        UIButton * upOrDownBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        upOrDownBtn.frame = CGRectMake(SCREEN_WIDTH - 15 - 25, 15, 25, 25);
        [upOrDownBtn setImage:[UIImage imageNamed:@"upward"] forState:UIControlStateNormal];
        [upOrDownBtn addTarget:self action:@selector(upOrDownAction:) forControlEvents:UIControlEventTouchUpInside];
        [_tableTopView addSubview:upOrDownBtn];
        
        UILabel * markLable = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(addCardBtn.frame), 15, SCREEN_WIDTH - 80, 25)];
        markLable.text = @"——— 我的信用卡 ———";
        markLable.textAlignment = NSTextAlignmentCenter;
        markLable.font = [UIFont systemFontOfSize:16];
        markLable.textColor = [UIColor colorWithHexString:@"2b2b2c"];
        [_tableTopView addSubview:markLable];
        
    }
    
    return _tableTopView;
}

- (void)addCardAction:(UIButton *)sender{
    
    if (identifyFlag == 1) {
        
        XJ_AddCreditCardViewController *addCreditCardVC =[[XJ_AddCreditCardViewController alloc]init];
        
        addCreditCardVC.userName = userName;
        addCreditCardVC.userIDNumber = userIDNumber;
        addCreditCardVC.channalID = channelId;
        
        [self.navigationController pushViewController:addCreditCardVC animated:YES];
    }else{
        
        XJ_RealNameAuthViewController * VC = [[XJ_RealNameAuthViewController alloc]init];
        VC.backRefurbishData = ^{
            
            [self requestHomeData];
        };
        
        [self.navigationController pushViewController:VC animated:YES];
    }
   
}

- (void)upOrDownAction:(UIButton *)sender{
    
    isOpen = !isOpen;
    
    if (isOpen == YES) {
        
        self.cardTable.frame = CGRectMake(15, CGRectGetMaxY(self.tableTopView.frame), SCREEN_WIDTH - 30, self.cardListArray.count * 190);
        self.addAcreditCardButton.frame = CGRectMake(15, CGRectGetMaxY(self.cardTable.frame) + 10, SCREEN_WIDTH - 30, 45);
        self.backScroll.contentSize = CGSizeMake(SCREEN_WIDTH, self.tableTopView.frame.origin.y + 50 + 190*self.cardListArray.count + 45 + 20);
        
    }else{
        
        self.cardTable.frame = CGRectMake(15, CGRectGetMaxY(self.tableTopView.frame), SCREEN_WIDTH - 30, 0);
        self.addAcreditCardButton.frame = CGRectMake(15, CGRectGetMaxY(self.cardTable.frame) + 10, SCREEN_WIDTH - 30, 45);
        self.backScroll.contentSize = CGSizeMake(SCREEN_WIDTH, self.tableTopView.frame.origin.y + 50 + 45 + 20);
    }
    
}
- (UIView *)emptyview{
    
    if (_emptyview == nil) {
        
        _emptyview = [[UIView alloc]initWithFrame:CGRectMake(15, 230, SCREEN_WIDTH - 30, 310)];
        _emptyview.backgroundColor = [UIColor whiteColor];
        _emptyview.layer.cornerRadius = 10;
        _emptyview.layer.shadowColor = [UIColor lightGrayColor].CGColor;
        _emptyview.layer.shadowOffset = CGSizeMake(5,5);
        _emptyview.layer.shadowOpacity = 0.8;//阴影透明度，默认0
        _emptyview.layer.shadowRadius = 4;//阴影半径，默认3
        _emptyview.hidden = YES;
        UIImageView * middleView = [[UIImageView alloc]initWithFrame:CGRectMake((_emptyview.frame.size.width - 150)/2, 40, 150, 130)];
        middleView.image = [UIImage imageNamed:@"bind_bg"];
        [_emptyview addSubview:middleView];
        
        UIButton * bindCardBtn = [[UIButton alloc]initWithFrame:CGRectMake(50, CGRectGetMaxY(middleView.frame)+25, _emptyview.frame.size.width - 100, 45)];
        bindCardBtn.layer.cornerRadius = 20;
        [bindCardBtn setBackgroundColor:[UIColor colorWithHexString:@"f98b3a"]];
        [bindCardBtn setTitle:@"绑定信用卡" forState:UIControlStateNormal];
        [bindCardBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [bindCardBtn addTarget:self action:@selector(addCardAction:) forControlEvents:UIControlEventTouchUpInside];
        [_emptyview addSubview:bindCardBtn];
        
        UILabel * markLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(bindCardBtn.frame)+10, SCREEN_WIDTH - 30, 15)];
        markLabel.text = @"绑定后可自动还款/定制还款计划";
        markLabel.font = [UIFont systemFontOfSize:15];
        markLabel.textAlignment = NSTextAlignmentCenter;
        markLabel.textColor = [UIColor lightGrayColor];
        [_emptyview addSubview:markLabel];
    }
    
    return _emptyview;
}

- (void)bindCardAction:(UIButton *)sender{
    
    
}

- (UIScrollView *)backScroll
{
    if (_backScroll == nil) {
        
        _backScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT - 49)];
        _backScroll.showsVerticalScrollIndicator = NO;
        _backScroll.delegate = self;
        _backScroll.backgroundColor = [UIColor colorWithHexString:@"f2f2f1"];
        // 禁止弹簧效果
//        _backScroll.bounces = NO;
    }
    
    return _backScroll;
}

- (UIScrollView *)bannerScroll
{
    if (_bannerScroll == nil) {
        
        _bannerScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_WIDTH/2)];
        _bannerScroll.showsHorizontalScrollIndicator = NO;
        _bannerScroll.delegate = self;
        _bannerScroll.backgroundColor = [UIColor clearColor];
       
    }
    
    return _bannerScroll;
}

- (void)viewWillAppear:(BOOL)animated{
    
   
}

-(void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
     [self requestHomeData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.nav_backImg.backgroundColor = [UIColor clearColor];
    isOpen = YES;//默认卡列表展开
   
    [self.view addSubview:self.backScroll];
    [self.backScroll addSubview:self.bannerScroll];
    [self topButtons];
    
    [self creatMainUI];
    
    [self loadUserCheckVersion];//检测版本
    
    NSUserDefaults * db = [NSUserDefaults standardUserDefaults];
    NSString * registerid = [db objectForKey:@"registerid"];
    
    if (registerid.length>0) {
        [self loadJPush:registerid];
    }
    // Do any additional setup after loading the view from its nib.
}

- (void)topButtons{
    
    UIButton * messageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    messageButton.frame = CGRectMake(SCREEN_WIDTH - 25 -15, 35, 25, 25);
    [messageButton setImage:[UIImage imageNamed:@"msg_ico"] forState:UIControlStateNormal];
    [messageButton addTarget:self action:@selector(pushNewsVC:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:messageButton];
}

- (void)pushNewsVC:(UIButton *)sender{
    
    XJ_HomeNewsViewController * newsVC = [[XJ_HomeNewsViewController alloc]init];
    [self.navigationController pushViewController:newsVC animated:YES];
}


-(void)creatMainUI{
    
    [self hideMySelfNavgation];
    
    [self addTopPagerView];
    
    if (notificationView == nil) {
        
        notificationView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_bannerScroll.frame), SCREEN_WIDTH, 35)];
        notificationView.backgroundColor = [UIColor whiteColor];
        [self.backScroll addSubview:notificationView];
        
        UIImageView * notiImage = [[UIImageView alloc]initWithFrame:CGRectMake(15, 8, 20, 20)];
        notiImage.image = [UIImage imageNamed:@"voice"];
        [notificationView addSubview:notiImage];
        
        UILabel * line = [[UILabel alloc]initWithFrame:CGRectMake(50, 8, 1, 20)];
        line.backgroundColor = [UIColor lightGrayColor];
        [notificationView addSubview:line];
        
        
        _scrollTextView = [[LMJScrollTextView2 alloc] initWithFrame:CGRectMake(CGRectGetMaxX(line.frame)+5, 0, SCREEN_WIDTH - line.frame.origin.x - 5 - 15, 35)];
        _scrollTextView.delegate            = self;
        _scrollTextView.textStayTime        = 1.5;
        _scrollTextView.scrollAnimationTime = 1;
        _scrollTextView.backgroundColor     = [UIColor clearColor];
        _scrollTextView.textColor           = [UIColor colorWithHexString:@"666666"];
        _scrollTextView.textFont            = [UIFont systemFontOfSize:14.0];
        _scrollTextView.textAlignment       = NSTextAlignmentLeft;
        _scrollTextView.touchEnable         = YES;
        _scrollTextView.layer.cornerRadius  = 3;
        [notificationView addSubview:_scrollTextView];
        
        
    }

    [self.backScroll addSubview:self.emptyview];
    [self.backScroll addSubview:self.tableTopView];
    [self.backScroll addSubview:self.cardTable];
    [self.backScroll addSubview:self.addAcreditCardButton];
    
    
}

#pragma 数据请求
- (void)requestHomeData{
    
    NSUserDefaults * db = [NSUserDefaults standardUserDefaults];
    NSString * token = [db objectForKey:@"token"];
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setValue:IDENTIFIER forKey:@"equipmentNo"];
    [dic setValue:APPVERSION forKey:@"appVersion"];
    [dic setValue:STSTERMVERSION forKey:@"systemVersion"];
    [dic setValue:USERID forKey:@"userId"];
    
    if (token!=nil &&![token isEqualToString:@""]) {
        
        [dic setValue:token forKey:@"token"];
        
    }
    
    [XJ_Utils signPost:@"user/index?" parameters:dic success:^(NSInteger recode, id jsonObject) {
        
        
        [self.playImagesArray removeAllObjects];
        [self.announceListArray removeAllObjects];
        [self.cardListArray removeAllObjects];
        
        
        NSDictionary * dataDic = [jsonObject objectForKey:@"data"];
        
        self->identifyFlag = [[dataDic objectForKey:@"identifyFlag"] integerValue];
        
        self->userName = [dataDic objectForKey:@"userName"];
        
        self->userIDNumber = [dataDic objectForKey:@"idNumber"];
        
        self->channelId = [[dataDic objectForKey:@"channelId"] integerValue];
        
        NSArray * cardListArr = [dataDic objectForKey:@"cardList"];
        
        NSArray * playImagesArr = [dataDic objectForKey:@"playImages"];
        
        NSArray * announceListArr = [dataDic objectForKey:@"announceList"];
        
        if (playImagesArr.count > 0) {
            
            for (NSDictionary * dic in playImagesArr) {
                
                XJ_HomePalyImageModel * playImageModel = [XJ_HomePalyImageModel mj_objectWithKeyValues:dic];
                [self.playImagesArray addObject:playImageModel];
            }
            
           
            [self loadData:self.playImagesArray];
        }
        
        if (announceListArr.count > 0) {
            
            for (NSDictionary * dic in announceListArr) {
                
                XJ_AnnounceListModel * announceListModel = [XJ_AnnounceListModel mj_objectWithKeyValues:dic];
                [self.announceListArray addObject:announceListModel.content];
            }

            self.scrollTextView.textDataArr =self.announceListArray;
            [self.scrollTextView startScrollBottomToTopWithNoSpace];
            
        }
        
        if (cardListArr.count>0) {
            
            self.tableTopView.hidden = NO;
            self.cardTable.hidden = NO;
            self.addAcreditCardButton.hidden = NO;
            self.emptyview.hidden = YES;
            
            for (NSDictionary * dic in cardListArr) {
                
                XJ_HomeCardListMode * cardListMode = [XJ_HomeCardListMode mj_objectWithKeyValues:dic];
                
                [self.cardListArray addObject:cardListMode];
                
            }
            
            self.cardTable.frame = CGRectMake(15, CGRectGetMaxY(self.tableTopView.frame), SCREEN_WIDTH - 30, self.cardListArray.count * 190);
            self.addAcreditCardButton.frame = CGRectMake(15, CGRectGetMaxY(self.cardTable.frame) + 10, SCREEN_WIDTH - 30, 45);
            self.backScroll.contentSize = CGSizeMake(SCREEN_WIDTH, self.tableTopView.frame.origin.y + 50 + 190*self.cardListArray.count + 45 + 20);
            
            [self.cardTable reloadData];
            
        }else{
            
            self.tableTopView.hidden = YES;
            self.cardTable.hidden = YES;
            self.addAcreditCardButton.hidden = YES;
            self.emptyview.hidden = NO;
        }
        
        
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
    
    
}

#pragma mark--tableDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.cardListArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 190;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XJ_CardListCell * cell = [tableView dequeueReusableCellWithIdentifier:@"XJ_CardListCell"];
    if (cell == nil) {
        
        cell = [[[NSBundle mainBundle]loadNibNamed:@"XJ_CardListCell" owner:self options:nil]objectAtIndex:0];
        
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    XJ_HomeCardListMode * model = [self.cardListArray objectAtIndex:indexPath.section];
    
    [cell setCellData:model];
    
    [cell.addHuanKuanBtn addTarget:self action:@selector(addHuanKunJiHuaAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.addHuanKuanBtn.tag = indexPath.section + 50;
    
    return cell;
    
}

- (void)addHuanKunJiHuaAction:(UIButton *)sender{
    
    NSInteger index = sender.tag - 50;
    
    XJ_HomeCardListMode * model = [self.cardListArray objectAtIndex:index];
    
    //backPlan  和  planUseFlag 两个字段判断，三种情况
    //backPlan   是 0 ，添加还款计划
    //backPlan    是1 ，planUseFlag  是1  还款计划列表
    //backPlan    是1 ，planUseFlag  是0 未提交计划列表
    
    
    if (model.backPlan == 0) {
        
        selectedCellNum = sender.tag - 50;
        
        [KWINDOW addSubview:self.leiXingControl];
        
    }else{
        
        if (model.planUseFlag == 0 && model.backPlan == 1) {
            
            XJ_UnsubmissionPlanListViewController * VC = [[XJ_UnsubmissionPlanListViewController alloc]init];
            VC.cardModel = model;
            [self.navigationController pushViewController:VC animated:YES];
            
        }else if (model.planUseFlag == 1 && model.backPlan == 1){
            
            XJ_ReturnListViewController * VC = [[XJ_ReturnListViewController alloc]init];
            VC.planID = model.planId;
            [self.navigationController pushViewController:VC animated:YES];
            
        }
    }
    
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XJ_HomeCardListMode * model = [self.cardListArray objectAtIndex:indexPath.section];
    
    XJ_HomeDetaiViewController *detail =[[XJ_HomeDetaiViewController alloc]init];
    detail.cardModel = model;
    [self.navigationController pushViewController:detail animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y <= 0)
    {
        CGPoint offset = scrollView.contentOffset;
        offset.y = 0;
        scrollView.contentOffset = offset;
    }
}
- (UIControl *)leiXingControl{
    
    if (_leiXingControl == nil) {
        
        _leiXingControl = [[UIControl alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _leiXingControl.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
        [_leiXingControl addTarget:self action:@selector(cancelControl) forControlEvents:UIControlEventTouchUpInside];
        XJ_JiHualeiXingView *jihualeixingView =[[XJ_JiHualeiXingView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-40, 208)];
        jihualeixingView.layer.position =self.view.center;
        
        [jihualeixingView.firstButton addTarget:self action:@selector(quickRepaymentAction:) forControlEvents:UIControlEventTouchUpInside];
        [_leiXingControl addSubview:jihualeixingView];
        
        
        
    }
    
    return _leiXingControl;
}
- (void)quickRepaymentAction:(UIButton *)sender{
    
    [self.leiXingControl removeFromSuperview];
    
    XJ_HomeCardListMode * model = [self.cardListArray objectAtIndex:selectedCellNum];
    
    XJ_QuickRepaymentViewController * quickRepaymentVC = [[XJ_QuickRepaymentViewController alloc]init];
    
    quickRepaymentVC.cardModel = model;
    
    [self.navigationController pushViewController:quickRepaymentVC animated:YES];
    
}
- (void)cancelControl{
    
    
    
    
    [self.leiXingControl removeFromSuperview];
}

#pragma lunbotu
- (void)addTopPagerView {
    
    TYCyclePagerView *pagerView = [[TYCyclePagerView alloc]init];
    pagerView.isInfiniteLoop = YES;
    pagerView.openTimer = YES;
    pagerView.autoScrollInterval = 3.0;
    pagerView.dataSource = self;
    pagerView.delegate = self;
    [pagerView registerClass:[TYCyclePagerViewCell class] forCellWithReuseIdentifier:@"cellId"];
    
    [self.bannerScroll addSubview:pagerView];
    _topPagerView = pagerView;
    
    TYPageControl *pageControl = [[TYPageControl alloc]init];
    pageControl.currentPageIndicatorSize = CGSizeMake(8, 8);
    [_topPagerView addSubview:pageControl];
    _pageControl = pageControl;
    
}
- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    float lunBoHeight =  (SCREEN_WIDTH/3)*1;
    float luntopHeight =  (SCREEN_WIDTH/10)*5;
    _topPagerView.frame = CGRectMake(0,0,SCREEN_WIDTH, luntopHeight);
    _pageControl.frame = CGRectMake(0, CGRectGetHeight(_topPagerView.frame) - 26, CGRectGetWidth(_topPagerView.frame), 26);
    
    _pagerView.frame = CGRectMake(0,0,SCREEN_WIDTH, lunBoHeight);
    
}
- (void)loadData:(NSArray *)bottomArray{
    
    NSMutableArray *datas = [NSMutableArray array];
    
    for (int i = 0; i < bottomArray.count; ++i) {
        
        XJ_HomePalyImageModel *attrib= [bottomArray objectAtIndex:i];
        [datas addObject:attrib.imageUrl];
    }
    
    Imagedatas = [datas copy];
    _pageControl.numberOfPages = Imagedatas.count;
    [_topPagerView reloadData];
    [_topPagerView scrollToItemAtIndex:0 animate:YES];
    
}


#pragma mark - TYCyclePagerViewDataSource

- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
    
    
    return self.playImagesArray.count;
    
}

- (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {
    TYCyclePagerViewCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndex:index];
    NSLog(@"Imagedatas[index] = %@",Imagedatas[index]);
    [cell.bImage sd_setImageWithURL:[NSURL URLWithString:Imagedatas[index]] placeholderImage:[UIImage imageNamed:@"bg"]];
    cell.bImage.userInteractionEnabled = YES;
    cell.bImage.tag = index +TOP_LUNBO;
    
    UITapGestureRecognizer* singleRecognizer;
    singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(homelunboSingleTap:)];
    //点击的次数
    singleRecognizer.view.tag = index;
    singleRecognizer.numberOfTapsRequired = 1; // 单击
    //给self.view添加一个手势监测；
    [cell.bImage addGestureRecognizer:singleRecognizer];
    
    return cell;
}

- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
    TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc]init];
    
    
    layout.itemSize = CGSizeMake(CGRectGetWidth(pageView.frame)*1.0, CGRectGetHeight(pageView.frame)*1.0);
    layout.itemHorizontalCenter = YES;
    return layout;
}

- (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    _pageControl.currentPage = toIndex;
    NSLog(@"%ld ->  %ld",fromIndex,toIndex);
}

- (void)homelunboSingleTap:(UIGestureRecognizer *)sender{
    
    NSInteger index = sender.view.tag - TOP_LUNBO;
    XJ_HomePalyImageModel *attrib= [self.playImagesArray objectAtIndex:index];
    XJ_ApplicationDetailViewController * VC = [[XJ_ApplicationDetailViewController alloc]init];
    VC.urlString = attrib.linkUrl;
    [self.navigationController pushViewController:VC animated:YES];
    
    
}
#pragma mark--检测更新接口
-(void)loadUserCheckVersion{
    
    NSUserDefaults * db = [NSUserDefaults standardUserDefaults];
    NSString * token = [db objectForKey:@"token"];
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setValue:IDENTIFIER forKey:@"equipmentNo"];
    [dic setValue:STSTERMVERSION forKey:@"systemVersion"];
    [dic setValue:APPVERSION forKey:@"appVersion"];
    [dic setValue:USERID forKey:@"userId"];
    [dic setValue:token forKey:@"token"];
    
    [XJ_Utils signPost:@"user/checkVersion?" parameters:dic success:^(NSInteger recode, id jsonObject) {
        
        NSDictionary * dataDic = [jsonObject objectForKey:@"data"];
        NSDictionary * iosVersionInfo = [dataDic objectForKey:@"iosVersionInfo"];
        NSString *updateLink =iosVersionInfo[@"updateLink"];//下载地址
        
        /////versionUpdateFlag是否更新：0：不需要更新 1：选择更新 2：强制更新
        NSInteger versionUpdateFlag =[iosVersionInfo[@"versionUpdateFlag"] integerValue];
        
        //线上构建版本
        NSInteger buildVersion = [iosVersionInfo[@"buildVersion"] integerValue];
        
        //本地构建版本
        NSString *localVersion = [[NSBundle mainBundle].infoDictionary objectForKey:@"CFBundleVersion"];
        
        if ([localVersion integerValue] < buildVersion ) {
            
            if (versionUpdateFlag ==1) {//选择更新
                
                XJ_RealNameAuthView *alext =[[XJ_RealNameAuthView alloc]initWithTitle:@"提示" Message:@"发现新版本，是否要更新？" sureBtn:@"确定" cancleBtn:@"取消"];
                alext.resultIndex = ^(NSInteger index) {
                    
                    if (index==2) {//到App Store下载页
                        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",updateLink]];
                        
                        [[UIApplication sharedApplication] openURL:url];
                    }
                    
                };
                [alext showXLAlertView];
                
                
            }else if (versionUpdateFlag ==2)//强制更新
            {
                XJ_RealNameAuthView *alext =[[XJ_RealNameAuthView alloc]initWithTitle:@"提示" Message:@"发现新版本，是否要更新？" sureBtn:@"确定" cancleBtn:nil];
                alext.resultIndex = ^(NSInteger index) {
                    
                    
                    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",updateLink]];
                    
                    [[UIApplication sharedApplication] openURL:url];
                    
                };
                [alext showXLAlertView];
                
                
            }else if(versionUpdateFlag == 0)//不需要提醒
            {
                return ;
            }
            
        }
        
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
    
}

#pragma mark--极光注册id接口
-(void)loadJPush:(NSString*)registerid{
    NSUserDefaults * db = [NSUserDefaults standardUserDefaults];
    NSString * token = [db objectForKey:@"token"];
    
    
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setValue:IDENTIFIER forKey:@"equipmentNo"];
    [dic setValue:STSTERMVERSION forKey:@"systemVersion"];
    [dic setValue:APPVERSION forKey:@"appVersion"];
    [dic setValue:USERID forKey:@"userId"];
    [dic setValue:token forKey:@"token"];
    
    
    [dic setValue:registerid forKey:@"registrationId"];
    
    
    [XJ_Utils signPost:@"/user/jPush?" parameters:dic success:^(NSInteger recode, id jsonObject) {
        
        NSLog(@"上传registrationId 成功");
    } failure:^(NSInteger recode, NSError *error) {
        NSLog(@"上传registrationId 失败");
    }];
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
